package Controller;

import Entity.UserEntity;
import Util.HibernateUtil;
import org.hibernate.Session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class Application {

    @RequestMapping("/hello")
    public String hello(){
        try {
            Session session = HibernateUtil.getSession();

//          数据库写操作
//
//            UserEntity user = new UserEntity();
//            user.setId(3);
//            user.setUsername("15");
//            user.setPassword("123456");
//            user.setAge(23);
//
//            session.beginTransaction();
//            session.save(user);
//            session.getTransaction().commit();
//
//            return "数据保存成功";

//          数据库读操作
            session.beginTransaction();
            UserEntity user = (UserEntity)session.get(UserEntity.class,1);
            session.close();

            return "hello," + user.getUsername();

//            return "hello,world!";

        }catch (Exception e){
            return "Error!";
        }
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
